#!/usr/bin/python
# Copyright 2007 pyPayPal Dev Team
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 only
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

import actions, objects, interfaces, paypal_exceptions, constants, sys
from optparse import OptionParser

def get_input(message,default):
    input = raw_input("%s (Default: %s): " %(message,default))
    if len(input):
        return input
    else:
        return default
    
def Setup_Direct_Payment_Sale():
    print "\nSetting up example Direct Payment Sale...\n"

    #Test Billing Address
    print ""
    print " * * * Enter Credit Card Billing address * * *"
    print "Note: Any field left blank will go to a default"

    address = objects.Billing_Address()
    address.first_name                  =   get_input("First Name", "Designer")
    address.last_name                   =   get_input("Last Name", "Fotos")
    address.address_line_one            =   get_input("Address Line One", "1234 Easy Street")
    address.address_line_two            =   get_input("Address Line Two", "")
    address.city                        =   get_input("City", "San Jose")
    address.state                       =   get_input("State", "CA")
    address.zipcode                     =   get_input("Zip", "95110")
    address.country                     =   get_input("Country", "United States")
    address.country_code                =   get_input("Country Code", "US")

    print " * * * Done entering Billing address * * *"
    
    #Test Shipping address
    ship_address = objects.Shipping_Address("McLovin", "Badass", "123 Fake St.", "Second Floor", "Denver", "CO", "80999", "US", "US")
    
    #Test Shopping Cart
    cart = objects.Cart()
    cart.add_item(objects.Item(name="test item 1", amount = 100.00, tax = 7.70, item_number = 666666))
    cart.add_item(objects.Item(name="test item 2", amount = 145.67, tax = 8.90, item_number = 888888))
    
    #Get Credit Card
    print ""
    print " * * * Enter Credit Card Information * * *"
    
    cc = objects.Credit_Card()
    cc.account          = get_input('Credit Card #', "4683075410516684")
    cc.cvv2             = get_input('CVV #', "808")
    cc.expiration_date  = get_input('Expires [MMYYYY]', "012007")
    cc.type             = get_input('Card type %s' %constants.VALID_CARDS, "Visa")
    
    print " * * * Finished Entering Card Information * * *"
    print ""
    
    #Get the Amount
    action = actions.Direct_Payment_Sale()
    action.billing_address = address
    action.credit_card = cc
    
    #Throw in the Subtotal as a test
    subtotal = objects.Subtotal()
    action.subtotal = subtotal


    print ""
    print " * * * Enter Payment attributes * * * "
    action.ip          =   get_input("IP","255.55.167.002")
    #Test Subtotal
    subtotal.subtotal = get_input('Enter Subtotal', '200.00')
    subtotal.tax = get_input('Enter Tax', '12.95')
    action.total   =   get_input("Enter Total", "212.95")
    #action.shipping_address = ship_address
    #action.cart = cart
    #action.total_amt = cart.get_subtotal() + cart.get_tax()
    return action

def Setup_Express_Checkout_Sale():
    print ''
    print "Testing Express_Checkout_Sale"
    print ''
    action = actions.Express_Checkout_Sale()
    action.return_url = get_input("Enter Return URL", "http://www.test.com/return")
    action.cancel_url = get_input("Enter Cancel URL", "http://www.test.com/cancel")
    action.total = get_input("Enter Total", "212.95")
    
    print action.get_uri()
    return action

parser = OptionParser()

parser.add_option("--user",        
                  dest = "username",  
                  action = "store",
                  default = None,
                  help = "PayPal Username.")

parser.add_option("--development", 
                  dest = "devel_mode",
                  action = "store_true",
                  default = True,
                  help = "Enable development mode (PayPal sandbox.) [Default]")

parser.add_option("--nvp", 
                  dest = "nvp_mode", 
                  action = "store_true",
                  default = True,
                  help = "Enable NVP mode. [Default]")



(options, args) = parser.parse_args()

# setup the Interface Credentials
if not options.username:
    print "Using Paypal Sample API signatures"
    print "    username:  sdk-three_api1.sdk.com"
    print "    password:  QFZCWN5HZM8VBG7Q"
    print "    signature: A-IzJhZZjhg29XQ2qnhapuwxIDzyAZQ92FRP5dqBzVesOkzbdUONzmOU"
    
    options.username = "sdk-three_api1.sdk.com"
    password = "QFZCWN5HZM8VBG7Q"
    signature = "A-IzJhZZjhg29XQ2qnhapuwxIDzyAZQ92FRP5dqBzVesOkzbdUONzmOU"        
else:
    #User Signature Inputs
    print ""
    print " * * * Enter Signature Parameters (you can get them @ developer.paypal.com) * * *"
    password = get_input("Password", "")
    signature = get_input("Signature", "")

print ""
print "\nInitializing our PayPal object...\n"
paypal_interface = None

if options.nvp_mode:
    # Use NVP
    paypal_interface = interfaces.PayPalNVP(user=options.username, passwd=password, signature=signature, development=options.devel_mode)
else:
    # Add other interface options
    pass
paypal_interface.set_gateway()

print "Object looks like:\n"
print paypal_interface.__dict__

# Now Run an example based on user input
#action = Setup_Direct_Payment_Sale()
action = Setup_Express_Checkout_Sale()
print "\nMaking request to PayPal...\n"
print paypal_interface.post_request(action)
print "\nClient Exit.\n"
#exit(0)

